package com.mpsc.current.config;

import com.mpsc.api.common.BaseResponse;
import com.mpsc.current.annotation.CacheLock;
import com.mpsc.current.common.CacheKeyGenerator;
import com.mpsc.current.common.RedissLockUtil;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.Method;
import java.util.List;

@Aspect
@Configuration
public class LockMethodInterceptor {
    @Autowired
    public LockMethodInterceptor(StringRedisTemplate lockRedisTemplate, CacheKeyGenerator cacheKeyGenerator) {
        this.lockRedisTemplate = lockRedisTemplate;
        this.cacheKeyGenerator = cacheKeyGenerator;
    }

    private final StringRedisTemplate lockRedisTemplate;

    private final CacheKeyGenerator cacheKeyGenerator;


    @Around("execution(public * *(..)) && @annotation(com.mpsc.current.annotation.CacheLock)")
    public Object interceptor(ProceedingJoinPoint pjp) {
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        Method method = signature.getMethod();
        CacheLock lock = method.getAnnotation(CacheLock.class);
        if (StringUtils.isEmpty(lock.prefix())) {
            throw new RuntimeException("lock key can't be null...");
        }
        final List<String> lockKeys = cacheKeyGenerator.getLockKey(pjp);
        try {
            if (!CollectionUtils.isEmpty(lockKeys)) {
                for (String lockKey : lockKeys) {
                    //key不存在才能设置成功,设置锁成功，执行业务逻辑
                    boolean success = RedissLockUtil.tryLock(lockKey, 0, -1);
//                    System.out.println(success + ":" + Thread.currentThread().getName() + ":" + lockKey);
//                    Thread.sleep(30000L);
                    if (success) {
                        //System.out.println("项目正常执行");
                        return pjp.proceed();
                    } else {
                        //传入的参数解析不到lockKey
                        //System.out.println("请勿重复请求");
                        return new BaseResponse(001,"请勿重复请求",null) ;
                    }
                }
            } else {
                return pjp.proceed();
            }
        } catch (Throwable throwable) {
            System.out.println(throwable.getMessage());
            return throwable.getMessage();
        } finally {
            System.out.println(Thread.currentThread().getName() + "开始关闭");
            if (!CollectionUtils.isEmpty(lockKeys)) {
                for (String lockKey : lockKeys) {
                    RedissLockUtil.unlock(lockKey);
                    System.out.println("unlock:" + lockKey);
                }
            }
        }
        return null;
    }
}
